home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / imap-3.0 / ANSI / c-client / mbox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-14  |  7.2 KB  |  230 lines

  1. /*
  2.  * Program:    MBOX mail routines
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    10 March 1992
  13.  * Last Edited:    15 July 1993
  14.  *
  15.  * Copyright 1993 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36. #include <stdio.h>
  37. #include <ctype.h>
  38. #include <netdb.h>
  39. #include <errno.h>
  40. extern int errno;        /* just in case */
  41. #include "mail.h"
  42. #include "osdep.h"
  43. #include <sys/file.h>
  44. #include <sys/stat.h>
  45. #include <sys/time.h>
  46. #include "mbox.h"
  47. #include "bezerk.h"
  48. #include "misc.h"
  49.  
  50. /* MBOX mail routines */
  51.  
  52.  
  53. /* Driver dispatch used by MAIL */
  54.  
  55. DRIVER mboxdriver = {
  56.   "mbox",            /* driver name */
  57.   (DRIVER *) NIL,        /* next driver */
  58.   mbox_valid,            /* mailbox is valid for us */
  59.   bezerk_parameters,        /* manipulate parameters */
  60.   mbox_find,            /* find mailboxes */
  61.   mbox_find,            /* find bboards */
  62.   mbox_find,            /* find all mailboxes */
  63.   mbox_find,            /* find all bboards */
  64.   bezerk_subscribe,        /* subscribe to mailbox */
  65.   bezerk_unsubscribe,        /* unsubscribe from mailbox */
  66.   bezerk_subscribe_bboard,    /* subscribe to bboard */
  67.   bezerk_unsubscribe_bboard,    /* unsubscribe from bboard */
  68.   bezerk_create,        /* create mailbox */
  69.   bezerk_delete,        /* delete mailbox */
  70.   bezerk_rename,        /* rename mailbox */
  71.   mbox_open,            /* open mailbox */
  72.   bezerk_close,            /* close mailbox */
  73.   bezerk_fetchfast,        /* fetch message "fast" attributes */
  74.   bezerk_fetchflags,        /* fetch message flags */
  75.   bezerk_fetchstructure,    /* fetch message structure */
  76.   bezerk_fetchheader,        /* fetch message header only */
  77.   bezerk_fetchtext,        /* fetch message body only */
  78.   bezerk_fetchbody,        /* fetch message body section */
  79.   bezerk_setflag,        /* set message flag */
  80.   bezerk_clearflag,        /* clear message flag */
  81.   bezerk_search,        /* search for message based on criteria */
  82.   mbox_ping,            /* ping mailbox to see if still alive */
  83.   mbox_check,            /* check for new messages */
  84.   mbox_expunge,            /* expunge deleted messages */
  85.   bezerk_copy,            /* copy messages to another mailbox */
  86.   bezerk_move,            /* move messages to another mailbox */
  87.   bezerk_append,        /* append string message to mailbox */
  88.   bezerk_gc            /* garbage collect stream */
  89. };
  90.  
  91.                 /* prototype stream */
  92. MAILSTREAM mboxproto = {&mboxdriver};
  93.  
  94. /* MBOX mail validate mailbox
  95.  * Accepts: mailbox name
  96.  * Returns: our driver if name is valid, NIL otherwise
  97.  */
  98.  
  99. DRIVER *mbox_valid (char *name)
  100. {
  101.   int fd;
  102.   char s[MAILTMPLEN];
  103.   char *t;
  104.   int ti,zn;
  105.   int ret = NIL;
  106.   struct stat sbuf;
  107.                 /* only consider INBOX */
  108.   if (!strcmp (ucase (strcpy (s,name)),"INBOX")) {
  109.                 /* make what the file name would be */
  110.     sprintf (s,"%s/mbox",myhomedir ());
  111.                 /* file exist? */
  112.     if ((stat(s,&sbuf) == 0) && (fd = open (s,O_RDONLY,NIL)) >= 0) {
  113.                 /* allow empty or valid file */
  114.       if ((sbuf.st_size == 0) || ((read (fd,s,MAILTMPLEN-1) >= 0) &&
  115.                   (*s == 'F') && VALID (s,t,ti,zn))) ret = T;
  116.       close (fd);        /* close the file */
  117.  
  118.     }
  119.   }
  120.   return ret ? &mboxdriver : NIL;
  121. }
  122.  
  123.  
  124. /* MBOX mail find list of mailboxes
  125.  * Accepts: mail stream
  126.  *        pattern to search
  127.  */
  128.  
  129. void mbox_find (MAILSTREAM *stream,char *pat)
  130. {
  131.   /* Always a no-op since bezerk will do this */
  132. }
  133.  
  134. /* MBOX mail open
  135.  * Accepts: stream to open
  136.  * Returns: stream on success, NIL on failure
  137.  */
  138.  
  139. MAILSTREAM *mbox_open (MAILSTREAM *stream)
  140. {
  141.   unsigned long i = 1;
  142.   unsigned long recent = 0;
  143.   char tmp[MAILTMPLEN];
  144.                 /* return prototype for OP_PROTOTYPE call */
  145.   if (!stream) return &mboxproto;
  146.                 /* change mailbox file name */
  147.   sprintf (tmp,"%s/mbox",myhomedir ());
  148.   fs_give ((void **) &stream->mailbox);
  149.   stream->mailbox = cpystr (tmp);
  150.   stream->silent = T;        /* don't babble on this stream */
  151.                 /* open mailbox, snarf new mail */
  152.   if (!(bezerk_open (stream) && mbox_ping (stream))) return NIL;
  153.   stream->silent = NIL;        /* allow external events again */
  154.                 /* notify upper level of mailbox sizes */
  155.   mail_exists (stream,stream->nmsgs);
  156.   while (i <= stream->nmsgs) if (mail_elt (stream,i++)->recent) ++recent;
  157.   mail_recent (stream,recent);    /* including recent messages */
  158.   return stream;
  159. }
  160.  
  161. /* MBOX mail ping mailbox
  162.  * Accepts: MAIL stream
  163.  * Returns: T if stream alive, else NIL
  164.  * No-op for readonly files, since read/writer can expunge it from under us!
  165.  */
  166.  
  167. long mbox_ping (MAILSTREAM *stream)
  168. {
  169.   int fd,sfd;
  170.   int ti,zn;
  171.   char *s,*t;
  172.   long size;
  173.   struct stat sbuf;
  174.   char lock[MAILTMPLEN],slock[MAILTMPLEN];
  175.   if (LOCAL && !stream->readonly && !stream->lock) {
  176.     mm_critical (stream);    /* go critical */
  177.                 /* calculate name of bezerk file */
  178.     sprintf (LOCAL->buf,MAILFILE,myusername ());
  179.     if ((sfd = bezerk_lock (LOCAL->buf,O_RDWR,NIL,slock,LOCK_EX)) >= 0) {
  180.       fstat (sfd,&sbuf);    /* get size of the poop */
  181.       if (size = sbuf.st_size){ /* non-empty? */
  182.                 /* yes, read it */
  183.     read (sfd,s = (char *) fs_get (sbuf.st_size + 1),size);
  184.     s[sbuf.st_size] = '\0';    /* tie it off */
  185.     if ((*s == 'F') && VALID (s,t,ti,zn)) {
  186.       if ((fd = bezerk_lock (stream->mailbox,O_WRONLY|O_APPEND,NIL,lock,
  187.                  LOCK_EX)) >= 0) {
  188.         fstat (fd,&sbuf);    /* get current file size before write*/
  189.         if (write (fd,s,size) >= 0) ftruncate (sfd,0);
  190.         else {        /* failed */
  191.           sprintf (LOCAL->buf,"New mail copy failed: %s",strerror (errno));
  192.           mm_log (LOCAL->buf,ERROR);
  193.           ftruncate (fd,sbuf.st_size);
  194.         }
  195.         fsync (fd);        /* force out the update */
  196.         bezerk_unlock (fd,NIL,lock);
  197.       }
  198.     }
  199.     else mm_log ("Invalid data in /usr/spool/mail file",ERROR);
  200.     fs_give ((void **) &s);    /* flush the poop now */
  201.       }
  202.                 /* all done with update */
  203.       bezerk_unlock (sfd,NIL,slock);
  204.     }
  205.     mm_nocritical (stream);    /* release critical */
  206.   }
  207.   return bezerk_ping (stream);    /* do the bezerk routine now */
  208. }
  209.  
  210. /* MBOX mail check mailbox
  211.  * Accepts: MAIL stream
  212.  */
  213.  
  214. void mbox_check (MAILSTREAM *stream)
  215. {
  216.                 /* do local ping, then do bezerk routine */
  217.   if (mbox_ping (stream)) bezerk_check (stream);
  218. }
  219.  
  220.  
  221. /* MBOX mail expunge mailbox
  222.  * Accepts: MAIL stream
  223.  */
  224.  
  225. void mbox_expunge (MAILSTREAM *stream)
  226. {
  227.   bezerk_expunge (stream);    /* do expunge */
  228.   mbox_ping (stream);        /* do local ping */
  229. }
  230.